package org.example.course.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.example.commonapi.domain.CourseSonChaptersVideo;
import org.example.commonapi.domain.R;
import org.example.commonapi.util.oss.OssService;
import org.example.course.service.CourseSonChaptersVideoService;
import org.example.course.until.CourseUntil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/course")
@Slf4j
public class CourseSonChaptersVideoController {

    @Resource
    private CourseSonChaptersVideoService courseSonChaptersVideoService;
    @Autowired
    private OssService ossService;

    /**
     * 根据大章节id查询课程下的所有章节
     * @param chapterId
     * @return
     */
    @GetMapping("/list/sonchapters/video/{chapterId}")
    public R listCourseSonChaptersVideo(@PathVariable Long chapterId) {
        LambdaQueryWrapper<CourseSonChaptersVideo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseSonChaptersVideo::getChapterId, chapterId);
        // 调用service方法
        List<CourseSonChaptersVideo> list=courseSonChaptersVideoService.list(queryWrapper);
        return R.success(list);
    }

    /**
     * 添加课程下的章节视频
     * @param courseSonChaptersVideo
     * @return
     */
    @PostMapping("/insert/sonchapters/video")
    public R insertCourseSonChaptersVideo(@RequestBody CourseSonChaptersVideo courseSonChaptersVideo) {
        R result = CourseUntil.isSonChaptersVideo(courseSonChaptersVideo);
        if (result!=null)return result;
        courseSonChaptersVideo.setStatus(0);
        courseSonChaptersVideoService.save(courseSonChaptersVideo);
        return R.success();
    }

    /**
     * 更新课程下的章节视频
     * @param id
     * @param file
     * @return
     */
    @PostMapping("/upload/sonchapters/video/{id}")
    public R uploadCourseSonChaptersVideo(@PathVariable Long id,@RequestPart("file") MultipartFile file) {
        String videoLink=ossService.upload(file);
        CourseSonChaptersVideo courseSonChaptersVideo=new CourseSonChaptersVideo();
        courseSonChaptersVideo.setId(id);
        courseSonChaptersVideo.setVideoLink(videoLink);
        courseSonChaptersVideoService.updateById(courseSonChaptersVideo);
        return R.success();
    }



    /**
     * 更新课程下的章节视频
     * @param courseSonChaptersVideo
     * @return
     */
    @PutMapping("/update/sonchapters/video")
    public R updateCourseSonChaptersVideo(@RequestBody CourseSonChaptersVideo courseSonChaptersVideo) {
        R result = CourseUntil.isSonChaptersVideo(courseSonChaptersVideo);
        if (result!=null)return result;
        courseSonChaptersVideoService.updateById(courseSonChaptersVideo);
        return R.success();
    }

    /**
     * 删除大章节下的小章节
     * @param id
     * @return
     */
    @DeleteMapping("/delete/sonchapters/video/{id}")
    public R deleteCourseSonChaptersVideo(@PathVariable Long id) {
        courseSonChaptersVideoService.removeById(id);
        return R.success();
    }

    /**
     * 根据id查询课程下的章节视频
     * @param id
     * @return
     */
    @GetMapping("/get/sonchapter/video/{id}")
    public R getCourseSonChaptersVideo(@PathVariable Long id) {
        CourseSonChaptersVideo courseSonChaptersVideo=courseSonChaptersVideoService.getById(id);
        return R.success(courseSonChaptersVideo);
    }
}
